package com.zhuzi.utils;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.service.model.BindingInfo;
import org.apache.cxf.service.model.BindingOperationInfo;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;

import javax.xml.namespace.QName;

/**
 * @author zhuzi
 * 2022/8/2 17:04
 */
public class ClientUtil {
    public static String callWebSVs(String wsdUrl, String operationName, Object... params) throws Exception {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(wsdUrl);
        //设置超时时间|单位是毫秒
        HTTPConduit conduit = (HTTPConduit) client.getConduit();
        HTTPClientPolicy policy = new HTTPClientPolicy();
        long timeout = 3 * 1000;//
        policy.setConnectionTimeout(timeout);
        policy.setReceiveTimeout(timeout);
        conduit.setClient(policy);
        //client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));


        // invoke("方法名",参数1,参数2,参数3....);
        Object[] objects = client.invoke(operationName, params);
        return objects[0].toString();
    }

    public static String callWebSVNameSpace(String wsdUrl, String nameSpace, String operation, Object... params) throws Exception {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(wsdUrl);
        //设置超时时间|单位是毫秒
        HTTPConduit conduit = (HTTPConduit) client.getConduit();
        HTTPClientPolicy policy = new HTTPClientPolicy();
        long timeout = 3 * 1000;//
        policy.setConnectionTimeout(timeout);
        policy.setReceiveTimeout(timeout);
        conduit.setClient(policy);

        //CXF动态客户端No operation was found with the name问题
        // 如果被调用的 接口和实现类不再同一个包下面 会报错No operation was found with the name
        // 1 接口和实现添加 targetNamespace
        // 2 调用时指定命名空间
        // 3 使用下面这段方法

        //client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        // invoke("方法名",参数1,参数2,参数3....);
        QName opName = new QName(nameSpace, operation);
        Object[] objects = client.invoke(opName, params);
        return objects[0].toString();
    }

    public static String callWebSV(String wsdUrl, String operation, Object... params) throws Exception {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(wsdUrl);

        //设置超时时间|单位是毫秒
        HTTPConduit conduit = (HTTPConduit) client.getConduit();
        HTTPClientPolicy policy = new HTTPClientPolicy();
        long timeout = 9 * 1000;//
        policy.setConnectionTimeout(timeout);
        policy.setReceiveTimeout(timeout);
        conduit.setClient(policy);
        //CXF动态客户端No operation was found with the name问题
        // 如果被调用的 接口和实现类不再同一个包下面 会报错No operation was found with the name
        // 1 接口和实现添加 targetNamespace
        // 2 调用时指定命名空间
        // 3 使用下面这段方法
        Endpoint endpoint = client.getEndpoint();
        QName opName = new QName(endpoint.getService().getName().getNamespaceURI(), operation);
        BindingInfo bindingInfo = endpoint.getEndpointInfo().getBinding();
        if (bindingInfo.getOperation(opName) == null) {
            for (BindingOperationInfo operationInfo : bindingInfo.getOperations()) {
                if (operation.equals(operationInfo.getName().getLocalPart())) {
                    opName = operationInfo.getName();
                    break;
                }
            }
        }
        //client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        // invoke("方法名",参数1,参数2,参数3....);
        System.out.println(opName);
        Object[] objects = client.invoke(opName, params);
        return objects[0].toString();
    }
}
